package com.uphone.magicleafteacher.pro.activity;

import android.graphics.Color;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.radish.baselibrary.Intent.IntentUtils;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.BaseListActivity;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.pro.adapter.FinishWorkAdapter;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.vo.HomeWorBean;
import com.uphone.magicleafteacher.vo.WorkcorrectionEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import okhttp3.ResponseBody;

public class FinishWorkActivity extends BaseListActivity<HomeWorBean.HomeWorksBean, FinishWorkAdapter> {

    @Override
    protected void initLayoutBefore() {
        super.initLayoutBefore();
        isEventBus = true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefresh(WorkcorrectionEvent outGroupEvent) {
        //刷新
        initData();
    }


    @Override
    protected Observable<ResponseBody> loadData(ApiService apiService) {
        return MyApp.apiService(ApiService.class)
                .homeWorks(CommonUtil.getTeacherId(), page + "", "10");
    }

    @Override
    protected List<HomeWorBean.HomeWorksBean> getDataList(String json) {
        HomeWorBean homeWorBean = GsonUtils.getGson().fromJson(json, HomeWorBean.class);
        if (homeWorBean != null)
            return homeWorBean.getHomeWorks();
        return null;
    }

    @Override
    protected void itemClick(FinishWorkAdapter adapter, View view, int position) {
        if (adapter.getItem(position).getProcessStatus() == 0) {
            ToastUtils.showShort("作业未完成");
        } else {//老师已批改
            IntentUtils.getInstance().with(this, WorkCorrectionActivity.class)
                    .putInt("status", adapter.getItem(position).getProcessStatus())
                    .putInt("processId", adapter.getItem(position).getProcessId())
                    .start();
        }
    }

    @Override
    protected FinishWorkAdapter initAdapter() {
        return new FinishWorkAdapter(R.layout.item_finishwork, new ArrayList<>());
    }

    @Override
    protected void initBundle() {

    }

    @Override
    protected void initTitle() {
        ll.setBackgroundColor(Color.WHITE);
        initTitle("作业批改");
    }
}
